
/* ==================================================================== */
/*    This File Defines All the Double Link Node/List Test Functions    */
/* ==================================================================== */

#include "d_link_list.h"
#include "test.h"

void show_test_info(void)
{
    printf("\r\n    Please input test id:\r\n");
    printf("        0 : to insert a new dl_node to the head\r\n");
    printf("        1 : to insert a new dl_node to the tail\r\n");
    printf("        2 : to delete a dl_node from the list\r\n");
    printf("        3 : to find a dl_node with specified data\r\n");
    printf("        4 : to change a dl_node'data \r\n");
    printf("        5 : to show all the nodes in the list\r\n");
    printf("        6 : to exit this test\r\n");
}



int test_insert_dl_node_to_head(DL_LIST *p_list)
{
    int user_input_data = 0;
    int user_tolerance  = TOLERANCE;

    printf("You are going to test insert dl_node to the head\r\n");

    printf("Please input the new dl_node's data\r\n");
    while(0 == scanf("%d", &user_input_data))
    {
        printf("Wrong Input, Try Again!\r\n");
        if(0 == (--user_tolerance))
        {
            printf("Test Fail, Are You Kidding Me!\r\n");
            user_tolerance = TOLERANCE;
            return -1;
        }
    }

    if(insert_dl_node_to_head(p_list, user_input_data))
    {
        printf("Test Fail!\r\n");
        return -1;
    }
}



int test_insert_dl_node_to_tail(DL_LIST *p_list)
{
    int user_input_data = 0;
    int user_tolerance  = TOLERANCE;
 
    printf("You are going to test insert dl_node to the tail\r\n");

    printf("Please input the new dl_node's data\r\n");
    while(0 == scanf("%d", &user_input_data))
    {
        printf("Wrong Input, Try Again!\r\n");
        if(0 == (--user_tolerance))
        {
            printf("Test Fail, Are You Kidding Me!\r\n");
            user_tolerance = TOLERANCE;
            return -1;
        }
    }
      
    if(insert_dl_node_to_tail(p_list, user_input_data))
    {
        printf("Test Fail!\r\n");
        return -1;
    }
}



int test_delete_dl_node(DL_LIST *p_list)
{
    int user_input_data = 0;
    int user_tolerance = TOLERANCE;

    printf("Now delete a dl_node from the list\r\n");
    printf("Please input the data of the dl_node to be deleted, only one dl_node is to be deleted\r\n");

    // this while loop is to see if the input data is valid
    while(0 == scanf("%d", &user_input_data))
    {
        printf("Wrong Input, Try Again!\r\n");
        if(0 == (--user_tolerance))
        {
            printf("Test Fail, Are You Kidding Me!\r\n");
            user_tolerance = TOLERANCE;
            return -1;
        }
    }
                
    if(delete_dl_node(p_list, user_input_data))
    {
        printf("Test Fail!\r\n");
        return -1;
    }

    return 0;
}   




int test_find_dl_node(DL_LIST *p_list)
{
    int user_input_data = 0;
    int user_tolerance = TOLERANCE;

    printf("Now find a dl_node from the list\r\n");
    printf("Please input the data of the dl_node to be found, only the first matched dl_node is available\r\n");

    // this while loop is to see if the input data is valid
    while(0 == scanf("%d", &user_input_data))
    {
        printf("Wrong Input, Try Again!\r\n");
        if(0 == (--user_tolerance))
        {
            printf("Test Fail, Are You Kidding Me!\r\n");
            user_tolerance = TOLERANCE;
            return -1;
        }
    }
                
    if(NULL == find_dl_node(p_list, user_input_data))
    {
		post_dl_event(NODE_NOT_FOUND, user_input_data);
		
		printf("post dl event NODE_NOT_FOUND\r\n");
       
		 return -1;
    }

    return 0;
}


int test_change_dl_node_data(DL_LIST *p_list)
{
    int user_input_old_data = 0;
    int user_input_new_data = 0;
    int user_tolerance = TOLERANCE;

    printf("Now change a dl_node's data\r\n");
    printf("Please input the data of the dl_node to be changed, only the first matched dl_node is available\r\n");

    // this while loop is to see if the input data is valid
    while(0 == scanf("%d", &user_input_old_data))
    {
        printf("Wrong Input, Try Again!\r\n");
        if(0 == (--user_tolerance))
        {
            printf("Test Fail, Are You Kidding Me!\r\n");
            user_tolerance = TOLERANCE;
            return -1;
        }
    }

    printf("Please input the new data\r\n");

    // this while loop is to see if the input data is valid
    while(0 == scanf("%d", &user_input_new_data))
    {
        printf("Wrong Input, Try Again!\r\n");
        if(0 == (--user_tolerance))
        {
            printf("Test Fail, Are You Kidding Me!\r\n");
            user_tolerance = TOLERANCE;
            return -1;
        }
    }

    if(change_dl_node_data(p_list, user_input_old_data, user_input_new_data))
    {
        return -1;
    }

    return 0;
}


